﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Sofire.Data;

namespace SofireXSqlEntityGenerator.Core
{
    public class SQLiteGenerator : SqlGeneratorBase
    {
        public SQLiteGenerator(QueryEngineBase engine) : base(engine) { }

        public override TableResult GetUserTables()
        {
            return this.Engine.ExecuteTable("select name as TABLE_NAME from sqlite_master where type='table' order by name");
        }

        public override TableResult GetUserViews()
        {
            return this.Engine.ExecuteTable("select name as VIEW_NAME from sqlite_master where type='view' order by name");
        }

        public override TableResult GetTableColumns(string tableName)
        {
            var tableResult =
             this.Engine.ExecuteTable(@"PRAGMA table_info('" + tableName + "')");
            if(tableResult.IsSucceed)
            {
                DataTable table = new DataTable();
                table.Columns.Add("FiledName", Types.String);
                table.Columns.Add("IsNullable", Types.Int32);
                table.Columns.Add("FieldComments", Types.String);

                foreach(DataRow row in tableResult.Value.Rows)
                {
                    table.Rows.Add(row["name"], row["notnull"], null);
                }
                tableResult.Value = table;
            }
            return tableResult;
        }
    }
}
